package com.ruoyi.quartz.task;

import com.ruoyi.common.enums.HdvStatusEnum;
import com.ruoyi.common.enums.OrgHistoryTypeEnum;
import com.ruoyi.common.enums.OrgRistTypeEnum;
import com.ruoyi.common.utils.CacheUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ruoyi.common.utils.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 定时任务调度测试
 *
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask
{

    @Autowired
    private IActionValueDataService iActionValueDataService;
    @Autowired
    private IROrgService iROrgService;
    @Autowired
    private IFireValueDataService iFireValueDataService;
    @Autowired
    private IToolValueDataService iToolValueDataService;
    @Autowired
    private IVehicleValueDataCService iVehicleValueDataCService;
    @Autowired
    private IVehicleValueDataService iVehicleValueDataService;
    @Autowired
    private IROrgRiskService iROrgRiskService;
    @Autowired
    private IROrgRiskHistoryService iROrgRiskHistoryService;
    @Autowired
    private IRiskLevelDataService iRiskLevelDataService;

    private static final Logger log = LoggerFactory.getLogger(RyTask.class);

    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params)
    {
        List<ROrg> rOrgs = iROrgService.selectROrgList(new ROrg());
        if(rOrgs.isEmpty()){
            return;
        }
//        upRiskLevelData(rOrgs);
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams()
    {
        List<ROrg> rOrgs = iROrgService.selectROrgList(new ROrg());
        if(rOrgs.isEmpty()){
            return;
        }
        System.out.println("执行无参方法");
        //删除缓存
        clearCache(rOrgs);
        //更新车辆统计
        try {
            countChe(rOrgs);
        }catch (Exception e){
            log.error("更新车辆统计异常："+e);
        }
//
//        //更新历史记录 查询当月，一个月初和月

        try {
            upHistory(rOrgs);
        }catch (Exception e){
            log.error("更新历史记录异常："+e);
        }
//
        //更新饼图 （车辆表 火灾表 行动表 工具表）

        try {
            updateRorgRisk(rOrgs);
        }catch (Exception e){
            log.error("更新饼图统计异常："+e);
        }

        //更新 risk_level_data

    }

    public void upRiskLevelData(List<ROrg> rOrgs){
        for (ROrg rOrg : rOrgs) {
            RiskLevelData riskLevelData = new RiskLevelData ();
            riskLevelData.setUnit(rOrg.getId());
            List<RiskLevelData> riskLevelData1 = iRiskLevelDataService.countRiskLevelDataList(riskLevelData);
            if(riskLevelData1.isEmpty()){
                continue;
            }
            List<RiskLevelData>   riskLevelDataDaoJue =   riskLevelData1.stream().filter(o -> o.getRistType().toString().equals(OrgRistTypeEnum.DAOJUE.getCode())).collect(Collectors.toList());
            saveRiskLevelData(riskLevelDataDaoJue);
            List<RiskLevelData>   riskLevelDataDaoQie = riskLevelData1.stream().filter(o -> o.getRistType().toString().equals(OrgRistTypeEnum.DAOQIE.getCode())).collect(Collectors.toList());
            saveRiskLevelData(riskLevelDataDaoQie);
            List<RiskLevelData>   riskLevelDataHuozai =   riskLevelData1.stream().filter(o -> o.getRistType().toString().equals(OrgRistTypeEnum.HUOZAI.getCode())).collect(Collectors.toList());
            saveRiskLevelData(riskLevelDataHuozai);
            List<RiskLevelData>   riskLevelDataFaren =   riskLevelData1.stream().filter(o -> o.getRistType().toString().equals(OrgRistTypeEnum.FARENWEIFA.getCode())).collect(Collectors.toList());
            saveRiskLevelData(riskLevelDataFaren);
        }
    }
    public void saveRiskLevelData( List<RiskLevelData>   riskLevelDatas){
        if(!riskLevelDatas.isEmpty()){
        Optional<RiskLevelData> collect = riskLevelDatas.stream().collect(Collectors.minBy((s1, s2) -> s1.getRistLevel() - s2.getRistLevel()));
        RiskLevelData riskLevelDataDaoJ = collect.get();
        riskLevelDataDaoJ.setAnomalousEventType(riskLevelDataDaoJ.getAnomalousEventType()+":"+riskLevelDataDaoJ.getID());
        riskLevelDataDaoJ.setID(null);
        int i = iRiskLevelDataService.insertRiskLevelData(riskLevelDataDaoJ);
        log.info("风险等级表入库:{},orgId:{},rist_type:{}",i,riskLevelDataDaoJ.getUnit(),riskLevelDataDaoJ.getRistType());
        }
    }

    public void countChe(List<ROrg> rOrgs){
        for (ROrg rOrg : rOrgs) {
            VehicleValueData vehicleValueData = new VehicleValueData ();
            vehicleValueData.setUnit(rOrg.getId());
            List<VehicleValueData> vehicleValueData1 = iVehicleValueDataService.selectCount6DataList(vehicleValueData);
            if(vehicleValueData1.isEmpty()){
                continue;
            }
            for (VehicleValueData valueData : vehicleValueData1) {
                if(!valueData.getHDV().equals(HdvStatusEnum.No.getInfo())&&valueData.getTotal()>6){
                    VehicleValueDataC vehicleValueDatac = new VehicleValueDataC();
                    BeanUtils.copyProperties(valueData,vehicleValueDatac);
                    int i = iVehicleValueDataCService.insertVehicleValueDataC(vehicleValueDatac);
                    log.info("车辆异常事件入库:{},orgId:{},chePai:{},HDV:{}",i,vehicleValueDatac.getUnit(),vehicleValueDatac.getPlateNumber(),vehicleValueDatac.getHDV());
                }else if(valueData.getHDV().equals(HdvStatusEnum.Yes.getInfo())){
                    VehicleValueDataC vehicleValueDatac = new VehicleValueDataC();
                    BeanUtils.copyProperties(valueData,vehicleValueDatac);
                    int i = iVehicleValueDataCService.insertVehicleValueDataC(vehicleValueDatac);
                    log.info("车辆异常事件入库:{},orgId:{},chePai:{},HDV:{}",i,vehicleValueDatac.getUnit(),vehicleValueDatac.getPlateNumber(),vehicleValueDatac.getHDV());
                }



            }

        }

    }
    public void upHistory(List<ROrg> rOrgs){
        for (ROrg rOrg : rOrgs) {
            ROrgRiskHistory rOrgRiskHistory = new ROrgRiskHistory ();
            rOrgRiskHistory.setOrgId(rOrg.getId());
            //消防风险
            List<ROrgRiskHistory> rOrgRiskHistories = iROrgRiskHistoryService.selectFireValueList(rOrgRiskHistory);
            if(rOrgRiskHistories.isEmpty()){
                continue;
            }
            for (ROrgRiskHistory orgRiskHistory : rOrgRiskHistories) {
                rOrgRiskHistory.setName(OrgHistoryTypeEnum.XFFX.getInfo());
                rOrgRiskHistory.setMonth(orgRiskHistory.getMonth());
                List<ROrgRiskHistory> rOrgRiskHistories1 = iROrgRiskHistoryService.selectROrgRiskHistoryList(rOrgRiskHistory);
                //没有则插入，有则更新
                if(rOrgRiskHistories1.isEmpty()){
                    orgRiskHistory.setName("消防风险");
                    orgRiskHistory.setId(null);
                    int i = iROrgRiskHistoryService.insertROrgRiskHistory(orgRiskHistory);
                    log.info("消防风险入库:{},orgId:{},cnt:{}",i,orgRiskHistory.getOrgId(),orgRiskHistory.getCnt());
                }else{
                    ROrgRiskHistory ROrgRiskHistory2 = rOrgRiskHistories1.get(0);
                    if(orgRiskHistory.getCnt()>ROrgRiskHistory2.getCnt()){
                        orgRiskHistory.setCnt(ROrgRiskHistory2.getCnt());
                        int i = iROrgRiskHistoryService.updateROrgRiskHistory(rOrgRiskHistory);
                        log.info("消防风险更新:{},orgId:{},cnt:{}",i,orgRiskHistory.getOrgId(),ROrgRiskHistory2.getCnt());
                    }
                }

            }
            //安防风险
            Long one = 0L;
            Long two = 0L;
            Long three = 0L;
            Long four = 0L;
            Long five = 0L;
            Long six = 0L;
            Long seven = 0L;
            Long eight = 0L;
            Long nine = 0L;
            Long ten = 0L;
            Long eleven = 0L;
            Long twelve = 0L;

            Long one1 = 0L;
            Long two1 = 0L;
            Long three1 = 0L;
            Long four1 = 0L;
            Long five1 = 0L;
            Long six1 = 0L;
            Long seven1 = 0L;
            Long eight1 = 0L;
            Long nine1 = 0L;
            Long ten1 = 0L;
            Long eleven1 = 0L;
            Long twelve1 = 0L;
            //车辆
            List<ROrgRiskHistory> rOrgRiskHistories3 = iROrgRiskHistoryService.selectVehicleValueList(rOrgRiskHistory);
            if(rOrgRiskHistories3.isEmpty()){
                continue;
            }
            for (ROrgRiskHistory orgRiskHistory : rOrgRiskHistories3) {
                ROrgRiskHistory orgRiskHistory0 = new ROrgRiskHistory ();
                orgRiskHistory0.setName(OrgHistoryTypeEnum.AFFX.getInfo());
                orgRiskHistory0.setMonth(orgRiskHistory.getMonth());
                orgRiskHistory0.setOrgId(orgRiskHistory.getOrgId());
                List<ROrgRiskHistory> rOrgRiskHistories2 = iROrgRiskHistoryService.selectROrgRiskHistoryList(orgRiskHistory0);
                if(rOrgRiskHistories2.isEmpty()){
                    switch (Integer.parseInt(orgRiskHistory.getMonth().toString())){
                        case 1: one1 = one1+orgRiskHistory.getCnt();break;
                        case 2: two1 = two1+orgRiskHistory.getCnt();break;
                        case 3: three1 = three1+orgRiskHistory.getCnt();break;
                        case 4: four1 = four1+orgRiskHistory.getCnt();break;
                        case 5: five1 = five1+orgRiskHistory.getCnt();break;
                        case 6: six1 = six1+orgRiskHistory.getCnt();break;
                        case 7: seven1 = seven1+orgRiskHistory.getCnt();break;
                        case 8: eight1 = eight1+orgRiskHistory.getCnt();break;
                        case 9: nine1 = nine1+orgRiskHistory.getCnt();break;
                        case 10: ten1 = ten1+orgRiskHistory.getCnt();break;
                        case 11: eleven1 = eleven1+orgRiskHistory.getCnt();break;
                        case 12: twelve1 = twelve1+orgRiskHistory.getCnt();break;
                    }

                }else{
                    switch (Integer.parseInt(orgRiskHistory.getMonth().toString())){
                        case 1: one = one+orgRiskHistory.getCnt();break;
                        case 2: two = two+orgRiskHistory.getCnt();break;
                        case 3: three = three+orgRiskHistory.getCnt();break;
                        case 4: four = four+orgRiskHistory.getCnt();break;
                        case 5: five = five+orgRiskHistory.getCnt();break;
                        case 6: six = six+orgRiskHistory.getCnt();break;
                        case 7: seven = seven+orgRiskHistory.getCnt();break;
                        case 8: eight = eight+orgRiskHistory.getCnt();break;
                        case 9: nine = nine+orgRiskHistory.getCnt();break;
                        case 10: ten = ten+orgRiskHistory.getCnt();break;
                        case 11: eleven = eleven+orgRiskHistory.getCnt();break;
                        case 12: twelve = twelve+orgRiskHistory.getCnt();break;
                    }
                }
            }
            //工具
            List<ROrgRiskHistory> rOrgRiskHistories4 = iROrgRiskHistoryService.selectToolValueList(rOrgRiskHistory);
            if(rOrgRiskHistories4.isEmpty()){
                continue;
            }
            for (ROrgRiskHistory orgRiskHistory : rOrgRiskHistories4) {
                ROrgRiskHistory orgRiskHistory0 = new ROrgRiskHistory ();
                orgRiskHistory0.setName(OrgHistoryTypeEnum.AFFX.getInfo());
                orgRiskHistory0.setMonth(orgRiskHistory.getMonth());
                orgRiskHistory0.setOrgId(orgRiskHistory.getOrgId());
                List<ROrgRiskHistory> rOrgRiskHistories2 = iROrgRiskHistoryService.selectROrgRiskHistoryList(orgRiskHistory0);
                if(rOrgRiskHistories2.isEmpty()){

                    switch (Integer.parseInt(orgRiskHistory.getMonth().toString())){
                        case 1: one1 = one1+orgRiskHistory.getCnt();break;
                        case 2: two1 = two1+orgRiskHistory.getCnt();break;
                        case 3: three1 = three1+orgRiskHistory.getCnt();break;
                        case 4: four1 = four1+orgRiskHistory.getCnt();break;
                        case 5: five1 = five1+orgRiskHistory.getCnt();break;
                        case 6: six1 = six1+orgRiskHistory.getCnt();break;
                        case 7: seven1 = seven1+orgRiskHistory.getCnt();break;
                        case 8: eight1 = eight1+orgRiskHistory.getCnt();break;
                        case 9: nine1 = nine1+orgRiskHistory.getCnt();break;
                        case 10: ten1 = ten1+orgRiskHistory.getCnt();break;
                        case 11: eleven1 = eleven1+orgRiskHistory.getCnt();break;
                        case 12: twelve1 = twelve1+orgRiskHistory.getCnt();break;
                    }
                }else{
                    switch (Integer.parseInt(orgRiskHistory.getMonth().toString())){
                        case 1: one = one+orgRiskHistory.getCnt();break;
                        case 2: two = two+orgRiskHistory.getCnt();break;
                        case 3: three = three+orgRiskHistory.getCnt();break;
                        case 4: four = four+orgRiskHistory.getCnt();break;
                        case 5: five = five+orgRiskHistory.getCnt();break;
                        case 6: six = six+orgRiskHistory.getCnt();break;
                        case 7: seven = seven+orgRiskHistory.getCnt();break;
                        case 8: eight = eight+orgRiskHistory.getCnt();break;
                        case 9: nine = nine+orgRiskHistory.getCnt();break;
                        case 10: ten = ten+orgRiskHistory.getCnt();break;
                        case 11: eleven = eleven+orgRiskHistory.getCnt();break;
                        case 12: twelve = twelve+orgRiskHistory.getCnt();break;
                    }
                }
            }
            //行动
            List<ROrgRiskHistory> rOrgRiskHistories1 = iROrgRiskHistoryService.selectActionValueList(rOrgRiskHistory);
            if(rOrgRiskHistories1.isEmpty()){
                continue;
            }
            for (ROrgRiskHistory orgRiskHistory : rOrgRiskHistories1) {
                ROrgRiskHistory orgRiskHistory0 = new ROrgRiskHistory ();
                orgRiskHistory0.setName(OrgHistoryTypeEnum.AFFX.getInfo());
                orgRiskHistory0.setMonth(orgRiskHistory.getMonth());
                orgRiskHistory0.setOrgId(orgRiskHistory.getOrgId());
                List<ROrgRiskHistory> rOrgRiskHistories2 = iROrgRiskHistoryService.selectROrgRiskHistoryList(orgRiskHistory0);
                if(rOrgRiskHistories2.isEmpty()){
                    switch (Integer.parseInt(orgRiskHistory.getMonth().toString())){
                        case 1: one1 = one1+orgRiskHistory.getCnt();break;
                        case 2: two1 = two1+orgRiskHistory.getCnt();break;
                        case 3: three1 = three1+orgRiskHistory.getCnt();break;
                        case 4: four1 = four1+orgRiskHistory.getCnt();break;
                        case 5: five1 = five1+orgRiskHistory.getCnt();break;
                        case 6: six1 = six1+orgRiskHistory.getCnt();break;
                        case 7: seven1 = seven1+orgRiskHistory.getCnt();break;
                        case 8: eight1 = eight1+orgRiskHistory.getCnt();break;
                        case 9: nine1 = nine1+orgRiskHistory.getCnt();break;
                        case 10: ten1 = ten1+orgRiskHistory.getCnt();break;
                        case 11: eleven1 = eleven1+orgRiskHistory.getCnt();break;
                        case 12: twelve1 = twelve1+orgRiskHistory.getCnt();break;
                    }
                }else{
                    switch (Integer.parseInt(orgRiskHistory.getMonth().toString())){
                        case 1: one = one+orgRiskHistory.getCnt();break;
                        case 2: two = two+orgRiskHistory.getCnt();break;
                        case 3: three = three+orgRiskHistory.getCnt();break;
                        case 4: four = four+orgRiskHistory.getCnt();break;
                        case 5: five = five+orgRiskHistory.getCnt();break;
                        case 6: six = six+orgRiskHistory.getCnt();break;
                        case 7: seven = seven+orgRiskHistory.getCnt();break;
                        case 8: eight = eight+orgRiskHistory.getCnt();break;
                        case 9: nine = nine+orgRiskHistory.getCnt();break;
                        case 10: ten = ten+orgRiskHistory.getCnt();break;
                        case 11: eleven = eleven+orgRiskHistory.getCnt();break;
                        case 12: twelve = twelve+orgRiskHistory.getCnt();break;
                    }

                }

            }

            rOrgRiskHistory.setName(OrgHistoryTypeEnum.AFFX.getInfo());
            List<ROrgRiskHistory> listss = iROrgRiskHistoryService.selectROrgRiskHistoryList(rOrgRiskHistory);
            if(!listss.isEmpty()){
                for (ROrgRiskHistory orgRiskHistory : listss) {
                    switch (Integer.parseInt(orgRiskHistory.getMonth().toString())){
                        case 1:
                            if(one>orgRiskHistory.getCnt()){
                                orgRiskHistory.setCnt(one);
                            }
                        case 2:
                            if(two>orgRiskHistory.getCnt()){
                                orgRiskHistory.setCnt(two);
                            }
                        case 3:
                            if(three>orgRiskHistory.getCnt()){
                                orgRiskHistory.setCnt(three);
                            }
                        case 4:
                            if(four>orgRiskHistory.getCnt()){
                                orgRiskHistory.setCnt(four);
                            }
                        case 5:
                            if(five>orgRiskHistory.getCnt()){
                                orgRiskHistory.setCnt(five);
                            }
                        case 6:
                            if(six>orgRiskHistory.getCnt()){
                                orgRiskHistory.setCnt(six);
                            }
                        case 7:
                            if(seven>orgRiskHistory.getCnt()){
                                orgRiskHistory.setCnt(seven);
                            }
                        case 8:
                            if(eight>orgRiskHistory.getCnt()){
                                orgRiskHistory.setCnt(eight);
                            }
                        case 9:
                            if(nine>orgRiskHistory.getCnt()){
                                orgRiskHistory.setCnt(nine);
                            }
                        case 10:
                            if(ten>orgRiskHistory.getCnt()){
                                orgRiskHistory.setCnt(ten);
                            }
                        case 11:
                            if(eleven>orgRiskHistory.getCnt()){
                                orgRiskHistory.setCnt(eleven);
                            }
                        case 12:
                            if(twelve>orgRiskHistory.getCnt()){
                                orgRiskHistory.setCnt(one);
                            }
                            int i = iROrgRiskHistoryService.updateROrgRiskHistory(orgRiskHistory);
                            log.info("安防风险更新:{},orgId:{},cnt:{}",i,orgRiskHistory.getOrgId(),orgRiskHistory.getCnt());

                    }
                }
            }else{
                if(one1>0){
                    rOrgRiskHistory.setMonth(1L);
                    rOrgRiskHistory.setCnt(one1);
                    int i = iROrgRiskHistoryService.insertROrgRiskHistory(rOrgRiskHistory);
                    log.info("安防风险入库:{},orgId:{},cnt:{},month:{}",i,rOrgRiskHistory.getOrgId(),rOrgRiskHistory.getCnt(),rOrgRiskHistory.getMonth());
                }
                if(two1>0){
                    rOrgRiskHistory.setMonth(2L);
                    rOrgRiskHistory.setCnt(two1);
                    int i = iROrgRiskHistoryService.insertROrgRiskHistory(rOrgRiskHistory);
                    log.info("安防风险入库:{},orgId:{},cnt:{},month:{}",i,rOrgRiskHistory.getOrgId(),rOrgRiskHistory.getCnt(),rOrgRiskHistory.getMonth());
                }
                if(three1>0){
                    rOrgRiskHistory.setMonth(3L);
                    rOrgRiskHistory.setCnt(three1);
                    int i = iROrgRiskHistoryService.insertROrgRiskHistory(rOrgRiskHistory);
                    log.info("安防风险入库:{},orgId:{},cnt:{},month:{}",i,rOrgRiskHistory.getOrgId(),rOrgRiskHistory.getCnt(),rOrgRiskHistory.getMonth());
                }
                if(four1>0){
                    rOrgRiskHistory.setMonth(4L);
                    rOrgRiskHistory.setCnt(four1);
                    int i = iROrgRiskHistoryService.insertROrgRiskHistory(rOrgRiskHistory);
                    log.info("安防风险入库:{},orgId:{},cnt:{},month:{}",i,rOrgRiskHistory.getOrgId(),rOrgRiskHistory.getCnt(),rOrgRiskHistory.getMonth());
                }
                if(five1>0){
                    rOrgRiskHistory.setMonth(5L);
                    rOrgRiskHistory.setCnt(five1);
                    int i = iROrgRiskHistoryService.insertROrgRiskHistory(rOrgRiskHistory);
                    log.info("安防风险入库:{},orgId:{},cnt:{},month:{}",i,rOrgRiskHistory.getOrgId(),rOrgRiskHistory.getCnt(),rOrgRiskHistory.getMonth());
                }
                if(six1>0){
                    rOrgRiskHistory.setMonth(6L);
                    rOrgRiskHistory.setCnt(six1);
                    int i = iROrgRiskHistoryService.insertROrgRiskHistory(rOrgRiskHistory);
                    log.info("安防风险入库:{},orgId:{},cnt:{},month:{}",i,rOrgRiskHistory.getOrgId(),rOrgRiskHistory.getCnt(),rOrgRiskHistory.getMonth());
                }
                if(seven1>0){
                    rOrgRiskHistory.setMonth(7L);
                    rOrgRiskHistory.setCnt(seven1);
                    int i = iROrgRiskHistoryService.insertROrgRiskHistory(rOrgRiskHistory);
                    log.info("安防风险入库:{},orgId:{},cnt:{},month:{}",i,rOrgRiskHistory.getOrgId(),rOrgRiskHistory.getCnt(),rOrgRiskHistory.getMonth());
                }
                if(eight1>0){
                    rOrgRiskHistory.setMonth(8L);
                    rOrgRiskHistory.setCnt(eight1);
                    int i = iROrgRiskHistoryService.insertROrgRiskHistory(rOrgRiskHistory);
                    log.info("安防风险入库:{},orgId:{},cnt:{},month:{}",i,rOrgRiskHistory.getOrgId(),rOrgRiskHistory.getCnt(),rOrgRiskHistory.getMonth());
                }
                if(nine1>0){
                    rOrgRiskHistory.setMonth(9L);
                    rOrgRiskHistory.setCnt(nine1);
                    int i = iROrgRiskHistoryService.insertROrgRiskHistory(rOrgRiskHistory);
                    log.info("安防风险入库:{},orgId:{},cnt:{},month:{}",i,rOrgRiskHistory.getOrgId(),rOrgRiskHistory.getCnt(),rOrgRiskHistory.getMonth());
                }
                if(ten1>0){
                    rOrgRiskHistory.setMonth(10L);
                    rOrgRiskHistory.setCnt(ten1);
                    int i = iROrgRiskHistoryService.insertROrgRiskHistory(rOrgRiskHistory);
                    log.info("安防风险入库:{},orgId:{},cnt:{},month:{}",i,rOrgRiskHistory.getOrgId(),rOrgRiskHistory.getCnt(),rOrgRiskHistory.getMonth());
                }
                if(eleven1>0){
                    rOrgRiskHistory.setMonth(11L);
                    rOrgRiskHistory.setCnt(eleven1);
                    int i = iROrgRiskHistoryService.insertROrgRiskHistory(rOrgRiskHistory);
                    log.info("安防风险入库:{},orgId:{},cnt:{},month:{}",i,rOrgRiskHistory.getOrgId(),rOrgRiskHistory.getCnt(),rOrgRiskHistory.getMonth());
                }
                if(twelve1>0){
                    rOrgRiskHistory.setMonth(12L);
                    rOrgRiskHistory.setCnt(twelve1);
                    int i = iROrgRiskHistoryService.insertROrgRiskHistory(rOrgRiskHistory);
                    log.info("安防风险入库:{},orgId:{},cnt:{},month:{}",i,rOrgRiskHistory.getOrgId(),rOrgRiskHistory.getCnt(),rOrgRiskHistory.getMonth());
                }
            }


        }

    }
    public void clearCache(List<ROrg> rOrgs){
        CacheUtils.remove(CacheUtils.PIE_KEY);
        CacheUtils.remove(CacheUtils.HISTORY_KEY);
        for (ROrg rOrg : rOrgs) {
            CacheUtils.remove(CacheUtils.EVENT_KEY+rOrg.getId());
        }

    }

    public void updateRorgRisk(List<ROrg> rOrgs){
        for (ROrg rOrg : rOrgs) {
            //动作表计数
            ActionValueData actionValueData = new ActionValueData();
            actionValueData.setUnit(Integer.parseInt(rOrg.getId().toString()));
            Long fireLong = iActionValueDataService.countActionValueDataList(actionValueData);

            List<ActionValueData> actionValueData1 = iActionValueDataService.lActionValueDataList(actionValueData);
            if(!actionValueData1.isEmpty()){
                for (ActionValueData valueData : actionValueData1) {
                    ROrgRisk rOrgRisk = new ROrgRisk ();
                    rOrgRisk.setOrgId(Long.valueOf(valueData.getUnit()));
                    rOrgRisk.setName(valueData.getActionType());
                    saveOrUpdateRorgRisk(rOrgRisk,valueData.getRistType());

                }
            }

            //火灾表
            FireValueData fireValueData = new FireValueData ();
            fireValueData.setUnit(rOrg.getId());
            Long actionLong = iFireValueDataService.countFireValueDataList(fireValueData);
            List<FireValueData> fireValueData1 = iFireValueDataService.lFireValueDataList(fireValueData);
            if(!fireValueData1.isEmpty()){
                for (FireValueData valueData : fireValueData1) {
                    ROrgRisk rOrgRisk = new ROrgRisk ();
                    rOrgRisk.setOrgId(Long.valueOf(valueData.getUnit()));
                    rOrgRisk.setName(valueData.getFireType());
                    saveOrUpdateRorgRisk(rOrgRisk,valueData.getRistType());
                }
            }


            //工具表
            ToolValueData toolValueData = new ToolValueData ();
            toolValueData.setUnit(Integer.parseInt(rOrg.getId().toString()));
            Long toolLong = iToolValueDataService.countToolValueDataList(toolValueData);
            List<ToolValueData> toolValueData1 = iToolValueDataService.lToolValueDataList(toolValueData);
            if(!toolValueData1.isEmpty()){
                for (ToolValueData valueData : toolValueData1) {
                    ROrgRisk rOrgRisk = new ROrgRisk ();
                    rOrgRisk.setOrgId(Long.valueOf(valueData.getUnit()));
                    rOrgRisk.setName(valueData.getToolType());
                    saveOrUpdateRorgRisk(rOrgRisk,valueData.getRistType());
                }
            }

            //车辆
            VehicleValueDataC vehicleValueDataC = new VehicleValueDataC ();
            vehicleValueDataC.setUnit(rOrg.getId());
            Long cheLong = iVehicleValueDataCService.countVehicleValueDataCList(vehicleValueDataC);
            List<VehicleValueDataC> vehicleValueDataCS = iVehicleValueDataCService.lVehicleValueDataCList(vehicleValueDataC);
            if(!vehicleValueDataCS.isEmpty()){
                for (VehicleValueDataC valueDataC : vehicleValueDataCS) {
                    ROrgRisk rOrgRisk = new ROrgRisk ();
                    rOrgRisk.setOrgId(Long.valueOf(valueDataC.getUnit()));
                    rOrgRisk.setName(valueDataC.getVehicleType());
                    saveOrUpdateRorgRisk(rOrgRisk,valueDataC.getRistType());
                }
            }

            ROrgRisk rOrgRisk = new ROrgRisk ();
            rOrgRisk.setOrgId(rOrg.getId());
            List<ROrgRisk> rOrgRisks = iROrgRiskService.selectROrgRiskList(rOrgRisk);

            if(rOrgRisks.isEmpty()){
                continue;
            }
            for (ROrgRisk orgRisk : rOrgRisks) {
                //count总数比org里面的多再更新，
                switch (orgRisk.getName()){
                    case "车辆异常":
                        if(cheLong>orgRisk.getCnt()){
                            orgRisk.setCnt(cheLong);
                        }
                    case "火灾异常":
                        if(fireLong>orgRisk.getCnt()){
                            orgRisk.setCnt(fireLong);
                        }
                    case "行人异常":
                        if(actionLong>orgRisk.getCnt()){
                            orgRisk.setCnt(actionLong);
                        }
                    case "工具异常":
                        if(toolLong>orgRisk.getCnt()){
                            orgRisk.setCnt(toolLong);
                        }
                }
                iROrgRiskService.updateROrgRisk(orgRisk);
            }




        }

    }


    public void saveOrUpdateRorgRisk(ROrgRisk rOrgRisk,Integer lev){
        List<ROrgRisk> rOrgRisks = iROrgRiskService.selectROrgRiskList(rOrgRisk);
        if(rOrgRisks.isEmpty()){
            rOrgRisk.setLev(lev);
            rOrgRisk.setCnt(1L);
            iROrgRiskService.insertROrgRisk(rOrgRisk);
        }else{
            for (ROrgRisk orgRisk : rOrgRisks) {
                orgRisk.setCnt(orgRisk.getCnt()+1);
                iROrgRiskService.updateROrgRisk(orgRisk);
            }

        }
    }
}
